package com.example.android_contentprovider;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_add;
    private Button btn_deleteAll;
    private Button btn_query;
    private Button btn_update;
    private ContentResolver cr;
    private EditText per_name, per_age;
    private static final String AUTHORITIES = "com.example.mycontentprovider.wang";
    private MyContentObserver observer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cr = getContentResolver();
        observer = new MyContentObserver(new Handler());
        cr.registerContentObserver(Uri.parse(uri), false, observer);
        initView();
    }

    public void initView() {
        btn_add = (Button) findViewById(R.id.btn_add);
        btn_deleteAll = (Button) findViewById(R.id.btn_delete);
        btn_query = (Button) findViewById(R.id.btn_query);
        btn_update = (Button) findViewById(R.id.btn_update);
        per_name = findViewById(R.id.name);
        per_age = findViewById(R.id.age);

        btn_add.setOnClickListener(this);
        btn_deleteAll.setOnClickListener(this);
        btn_query.setOnClickListener(this);
        btn_update.setOnClickListener(this);
    }

    private String uri = "content://" + AUTHORITIES + "/person";
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add:
                new Thread(){
                    public void run() {
                        //休眠3秒，模拟异步任务
                        SystemClock.sleep(3000);
                        add();
                    };
                }.start();
                break;
            case R.id.btn_delete:
                Log.e("MainActivity", "删除名字为Tom的数据");

                String delName = per_name.getText().toString();

                if( "".equals( delName)) {
                    Toast.makeText(this, "(删除时)姓名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                cr.delete(Uri.parse(uri), "name = ?", new String[]{delName});
                clearText();
                break;
            case R.id.btn_query:
                Cursor cursor = cr.query(Uri.parse(uri), null, null, null, null);
                ArrayList<Person> persons = new ArrayList<Person>();
                while (cursor.moveToNext()) {
                    int _id = cursor.getInt(0);
                    String name = cursor.getString(1);
                    int age = cursor.getInt(2);
                    persons.add(new Person(_id, name, age));
                }
                Toast.makeText(this, " log.e 日志打印全部信息(无需填写查询信息)", Toast.LENGTH_SHORT).show();
                for (Person person : persons) {
                    Log.e("MainActivity", person.toString());
                }
                break;
            case R.id.btn_update:

                Toast.makeText(this, "更改最后一条数据的name为PALL", Toast.LENGTH_SHORT).show();
                ContentValues values2 = new ContentValues();
                values2.put("name", "PALL");
                //获取数据库的行数
                Cursor cursor2 = cr.query(Uri.parse(uri), null, null, null, null);
                int count = cursor2.getCount();
                cr.update(Uri.parse(uri), values2, "_id = ?", new String[]{count + ""});
                break;

            default:
                break;
        }
    }

    // 添加数据 person类
    private void add() {
        Log.e("MainActivity", "添加一条name为Tom,age为21的数据");
        String name = per_name.getText().toString();
        String age = per_age.getText().toString();

        if( "".equals(name) || "".equals(age)) {
            Toast.makeText(this, "所填信息不能为空", Toast.LENGTH_SHORT).show();
            clearText();
            return;
        }

        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("age", age);
        cr.insert(Uri.parse(uri), values);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                clearText();
            }
        });
    }

    private void clearText() {
        per_name.setText("");
        per_age.setText("");
    }

    private class MyContentObserver extends ContentObserver {
        public MyContentObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            Toast.makeText(getApplicationContext(), "数据改变啦!!!", Toast.LENGTH_SHORT).show();
            super.onChange(selfChange);
        }
    }

}
